﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace One_day.Areas.admin.Controllers
{
    public class ProductController : Controller
    {
        // GET: admin/Product
        public ActionResult Index()
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            List<One_day.Models.Table> lst = db.Table.ToList();
            ViewBag.lst = lst;
            return View();
        }
        /// <summary>
        /// 显示一个界面，用来填写数据
        /// </summary>
        /// <returns></returns>
        public ActionResult Add()//需要传递分类列表，供下拉框使用
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            List<One_day.Models.T_Shop_Category> cate = db.T_Shop_Category.ToList();
            ViewBag.cate = cate;
            return View();
        }
        /// <summary>
        /// 进行数据保存
        /// </summary>
        /// <returns></returns>
        public ActionResult AddSave(String ProductName,int Price,int SelledAmount,int StockAmount, int CategoryId,HttpPostedFileBase PicUrl)
        {
            string filename = "";
            if (PicUrl == null || String.IsNullOrEmpty(PicUrl.FileName) || PicUrl.ContentLength == 0)
            {

            }
            else
            {
                filename = System.IO.Path.GetFileName(PicUrl.FileName);
                string virtualPath = String.Format("~/Upload/{0}", filename);
                string path = Server.MapPath(virtualPath);
                PicUrl.SaveAs(path);
            }
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.Table product = new Models.Table();
            product.CategoryId = CategoryId;
            product.PicUrl = filename;
            product.Price = Price;
            product.SelledAmount = SelledAmount;
            product.StockAmount = StockAmount;
            product.ProductName = ProductName;
            db.Table.Add(product);
            db.SaveChanges();
            return Redirect("/admin/product/index"); ;
        }
        /// <summary>
        /// 删除数据
        /// </summary>
        /// <returns></returns>
        public JsonResult Delete(int id)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.Table product = db.Table.Single(m => m.Id == id);
            int result;
            try
            {
                db.Table.Remove(product);
                result=db.SaveChanges();
            }
            catch
            {
                return Json( new {code=200,message="数据正在被使用，无法删除"});
            }
            if (result > 0)
                return Json(new { code = 100, message = "删除成功" });
            else
                return Json(new { code = 300, message = "程序异常出错" });
        }
        /// <summary>
        /// 修改商品信息
        /// </summary>
        /// <returns></returns>
        public ActionResult Update(int id)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.Table product = db.Table.Single(m => m.Id == id);
            List<One_day.Models.T_Shop_Category> cate = db.T_Shop_Category.ToList();
            ViewBag.category = cate;
            ViewBag.product = product;
            return View();
        }
        /// <summary>
        /// 保存  修改信息时修改的 内容
        /// </summary>
        /// <returns></returns>
        public ActionResult UpdateSave(String ProductName, int Price, int SelledAmount, int StockAmount, int CategoryId, HttpPostedFileBase PicUrl,int id)
        {
            One_day.Models.YichenEntities db = new Models.YichenEntities();
            One_day.Models.Table product = db.Table.Single(m => m.Id == id);
            string filename = product.PicUrl ;
            if (PicUrl == null || String.IsNullOrEmpty(PicUrl.FileName) || PicUrl.ContentLength == 0)
            {

            }
            else
            {
                filename = System.IO.Path.GetFileName(PicUrl.FileName);
                string virtualPath = String.Format("~/Upload/{0}", filename);
                string path = Server.MapPath(virtualPath);
                PicUrl.SaveAs(path);
            }
            product.CategoryId = CategoryId;
            product.PicUrl = filename;
            product.Price = Price;
            product.SelledAmount = SelledAmount;
            product.StockAmount = StockAmount;
            product.ProductName = ProductName;
            db.SaveChanges();
            return Redirect("/admin/product/index");
        }
    }
}